package com.kawins.mina.net;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class HttpRequestMessage {
	private Map<String, String> headers;
	private Map<String, String> parameters = null;

	public HttpRequestMessage() {
		this.headers = new HashMap<String, String>();
		this.parameters = new HashMap<String, String>();
	}

	public HttpRequestMessage(Map<String, String> headers, Map<String, String> parameters) {
		this.headers = headers;
		this.parameters = parameters;
	}

	public Map<String, String> getHeaders() {
		return this.headers;
	}

	public String getHeader(String key) {
		return this.headers.get(key);
	}

	public Map<String, String> getParameters() {
		return this.parameters;
	}

	public void setParameters(Map<String, String> parameters) {
		this.parameters = parameters;
	}

	public String getContext() {
		String context = this.headers.get("Context");
		return context == null ? "" : context;
	}

	public String getParameter(String name) {
		String param = this.parameters.get(name);
		return param == null ? "" : param;
	}

	@Override
	public String toString() {
		StringBuilder str = new StringBuilder();
		for (Map.Entry<String, String> e : this.headers.entrySet()) {
			str.append((String) e.getKey() + " : " + (String) e.getValue() + "\n");
		}
		for (Map.Entry<String, String> e : this.parameters.entrySet()) {
			str.append("@" + (String) e.getKey() + " : " + (String) e.getValue() + "\n");
		}
		return str.toString();
	}

	public String getParamURLEncoder() {
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> e : this.parameters.entrySet()) {
			sb.append((String) e.getKey());
			sb.append("=");
			try {
				sb.append(URLEncoder.encode((String) e.getValue(), "utf-8"));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			sb.append("&");
		}
		String line = sb.toString();
		return line;
	}

}
